<template>
  <div class="reports">
    <el-container>
      <el-header>
        <div class="header-content">
          <h1>Smart API Tester</h1>
          <el-menu mode="horizontal" :default-active="activeIndex" router>
            <el-menu-item index="/">首页</el-menu-item>
            <el-menu-item index="/projects">项目管理</el-menu-item>
            <el-menu-item index="/test-cases">测试用例</el-menu-item>
            <el-menu-item index="/reports">测试报告</el-menu-item>
          </el-menu>
        </div>
      </el-header>
      <el-main>
        <div class="page-header">
          <h2>测试报告</h2>
          <div>
            <el-select v-model="selectedProject" placeholder="选择项目" style="width: 200px; margin-right: 10px;">
              <el-option
                v-for="project in projects"
                :key="project.id"
                :label="project.name"
                :value="project.id"
              />
            </el-select>
            <el-button type="primary" @click="filterReports">
              <el-icon><search /></el-icon>
              筛选
            </el-button>
          </div>
        </div>
        
        <el-table :data="reports" style="width: 100%">
          <el-table-column prop="name" label="报告名称" />
          <el-table-column prop="project_name" label="所属项目" />
          <el-table-column prop="created_at" label="生成时间" />
          <el-table-column prop="total_cases" label="总用例数" width="100" />
          <el-table-column prop="passed_cases" label="通过数" width="100" />
          <el-table-column prop="failed_cases" label="失败数" width="100" />
          <el-table-column prop="pass_rate" label="通过率" width="100" />
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <el-button size="small" @click="viewReport(scope.row)">查看</el-button>
              <el-button size="small" @click="downloadReport(scope.row)">下载</el-button>
              <el-button size="small" type="danger" @click="deleteReport(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 报告详情对话框 -->
        <el-dialog
          v-model="showReportDialog"
          title="测试报告详情"
          width="80%"
        >
          <div v-if="currentReport">
            <el-descriptions :column="2" border>
              <el-descriptions-item label="报告名称">{{ currentReport.name }}</el-descriptions-item>
              <el-descriptions-item label="所属项目">{{ currentReport.project_name }}</el-descriptions-item>
              <el-descriptions-item label="生成时间">{{ currentReport.created_at }}</el-descriptions-item>
              <el-descriptions-item label="总用例数">{{ currentReport.total_cases }}</el-descriptions-item>
              <el-descriptions-item label="通过数">{{ currentReport.passed_cases }}</el-descriptions-item>
              <el-descriptions-item label="失败数">{{ currentReport.failed_cases }}</el-descriptions-item>
              <el-descriptions-item label="通过率">{{ currentReport.pass_rate }}</el-descriptions-item>
              <el-descriptions-item label="执行时长">{{ currentReport.duration }}</el-descriptions-item>
            </el-descriptions>
            
            <div class="chart-container">
              <v-chart class="chart" :option="chartOption" />
            </div>
            
            <el-table :data="currentReport.test_results" style="width: 100%; margin-top: 20px;">
              <el-table-column prop="test_case_name" label="测试用例" />
              <el-table-column prop="method" label="请求方法" width="100" />
              <el-table-column prop="url" label="请求URL" />
              <el-table-column prop="status" label="状态" width="100">
                <template #default="scope">
                  <el-tag :type="scope.row.status === 'passed' ? 'success' : 'danger'">
                    {{ scope.row.status === 'passed' ? '通过' : '失败' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="response_time" label="响应时间(ms)" width="120" />
              <el-table-column prop="error_message" label="错误信息" />
            </el-table>
          </div>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart } from 'echarts/charts'
import { TitleComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import VChart from 'vue-echarts'

// 注册必要的组件
use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent
])

export default {
  name: 'Reports',
  components: {
    Search,
    VChart
  },
  setup() {
    const activeIndex = ref('/reports')
    const selectedProject = ref(null)
    const showReportDialog = ref(false)
    const currentReport = ref(null)
    
    const reports = ref([
      {
        id: 1,
        name: '示例项目测试报告',
        project_name: '示例项目',
        created_at: '2023-01-01 10:00:00',
        total_cases: 10,
        passed_cases: 8,
        failed_cases: 2,
        pass_rate: '80%',
        duration: '2分30秒',
        test_results: [
          {
            test_case_name: '获取用户列表',
            method: 'GET',
            url: '/api/users',
            status: 'passed',
            response_time: 120,
            error_message: ''
          },
          {
            test_case_name: '创建用户',
            method: 'POST',
            url: '/api/users',
            status: 'failed',
            response_time: 500,
            error_message: '400 Bad Request'
          }
        ]
      }
    ])
    
    const projects = ref([
      {
        id: 1,
        name: '示例项目'
      }
    ])
    
    const chartOption = computed(() => {
      if (!currentReport.value) return {}
      
      return {
        title: {
          text: '测试结果分布',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '测试结果',
            type: 'pie',
            radius: '50%',
            data: [
              { value: currentReport.value.passed_cases, name: '通过' },
              { value: currentReport.value.failed_cases, name: '失败' }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    })

    const filterReports = () => {
      console.log('筛选报告:', selectedProject.value)
    }

    const viewReport = (report) => {
      currentReport.value = report
      showReportDialog.value = true
    }

    const downloadReport = (report) => {
      console.log('下载报告:', report)
    }

    const deleteReport = (report) => {
      console.log('删除报告:', report)
    }

    return {
      activeIndex,
      selectedProject,
      showReportDialog,
      currentReport,
      reports,
      projects,
      chartOption,
      filterReports,
      viewReport,
      downloadReport,
      deleteReport
    }
  }
}
</script>

<style scoped>
.reports {
  height: 100vh;
}

.el-header {
  background-color: #409EFF;
  color: white;
  padding: 0;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.header-content h1 {
  margin: 0;
  font-size: 24px;
}

.el-menu {
  background-color: transparent;
  border-bottom: none;
}

.el-menu-item {
  color: white;
  border-bottom: 2px solid transparent;
}

.el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.el-menu-item.is-active {
  border-bottom-color: white;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #303133;
}

.page-header div {
  display: flex;
  align-items: center;
}

.chart-container {
  margin-top: 20px;
  height: 300px;
}

.chart {
  height: 100%;
  width: 100%;
}
</style>